package com.luhuiguo.cms.server.handler;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.service.ArticleService;
import com.luhuiguo.cms.shared.rpc.RemoveArticles;
import com.luhuiguo.cms.shared.rpc.RemoveArticlesResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class RemoveArticlesHandler implements ActionHandler<RemoveArticles, RemoveArticlesResult> {

    @Autowired
    private ArticleService articleService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public RemoveArticlesResult execute(RemoveArticles action, ExecutionContext context) throws ActionException {

        List<Long> list = action.get();

        for (Long id : list) {
            articleService.deleteArticle(id);
        }

        return new RemoveArticlesResult(list.size());

    }

    @Override
    public Class<RemoveArticles> getActionType() {
        return RemoveArticles.class;
    }

    @Override
    public void rollback(RemoveArticles action, RemoveArticlesResult result, ExecutionContext context)
            throws ActionException {

    }

}
